package org.example;

import org.example.bean.User;
import org.example.config.Config;
import org.example.config.ConfigBean;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

    public static void main(String[] args) {
        /**
         * 1. <bean></bean>
         * 2. @Bean
         * 3. @Component
         * 4. BeanDefinition
         */

         //第一种 xml配置的形式
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        User user = context.getBean("user",User.class);
        System.out.println(user.getClass().getName()); //org.example.bean.User$$EnhancerBySpringCGLIB$$a4bf98a4

        /* 第二种 利用注解 java配置类的方法配置@Bean
        AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(ConfigBean.class);
        User user = annotationContext.getBean("getUser", User.class);
        System.out.println(user);*/

        /*第三种 利用注解 java配置类的上加@ComponentScan(包名) 再将需要交给spring容器管理的类上加@Component
        AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(Config.class);
        User user = annotationContext.getBean("user", User.class);
        System.out.println(user);*/

        /*// 第四种 利用BeanDefinition
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition.setBeanClass(User.class);
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.registerBeanDefinition("user", beanDefinition);
        applicationContext.refresh();
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user);*/

    }
}
